from flask import Blueprint, request
from models import Buy
from service import buyService

buy_bp = Blueprint('buy_bp', __name__)


#查询所有用户的互借信息
@buy_bp.route('/', methods=['GET'])
def getAllBuys():
    return buyService.getAllBuyList()


#根据user_id获取用户互借信息
@buy_bp.route('/<int:user_id>', methods=['GET'])
def getBuyByUserId(user_id):
    return buyService.getBuyByUserId(user_id)


@buy_bp.route('/', methods=['POST'])
def createBuy():
    data = request.get_json()
    buy = Buy(data['user_from_id'], data['user_to_id'], data['library_name'], data['num'], data['location'])
    return buyService.creatBuy(buy)


@buy_bp.route('/return', methods=['POST'])
def returnBuy():
    data = request.get_json()
    buy = Buy(data['user_from_id'], data['user_to_id'], data['library_name'], data['num'], data['location'])
    id=data['id']
    return buyService.returnBuy(id,buy)